home *** CD-ROM | disk | FTP | other *** search
- Path: lyra.csx.cam.ac.uk!93heb
- From: 93heb@eng.cam.ac.uk (H.E. Butterworth)
- Newsgroups: comp.lang.c
- Subject: Re: How to access memory allocated in function
- Date: 15 Jan 1996 14:14:18 GMT
- Organization: Cambridge University Engineering Department, UK
- Message-ID: <4ddnfq$ceh@lyra.csx.cam.ac.uk>
- References: <4ddbe3$so3@josie.abo.fi>
- NNTP-Posting-Host: tw800.eng.cam.ac.uk
-
- In article <4ddbe3$so3@josie.abo.fi>, csundqvi@abo.fi (Christoffer Sundqvist) writes:
- > Hello
- >
- >
- > How can i access memory allocated dynamically in a function after i leave the
- > function, something like this:
- >
- > main()
- > {
- > int *p;
- > sub(p);
- > }
- >
- > void sub(int *p)
- > {
- > p = malloc(.....);
- > }
- >
- > Thanks !
-
-
- How about:
-
- void sub( int ** ppI )
- {
- *ppI = malloc( sizeof( int ) );
-
- if( *ppI != NULL )
- {
- **ppI = 42;
- }
- }
-
- main()
- {
- int * pI;
-
- sub( &pI );
-
- if( pI != NULL )
- {
- printf( "%d\n", *pI );
- }
- else
- {
- printf( "malloc failed" );
- }
- }
-
- which should print the answer to the ultimate question of life the universe and
- everything - if given enough memory :-).
-
- HB.
-